import permission  from '@/api/permission'

const state = {
  list: [],
  page: {},
  detail: {},
}

const mutations = {

  SET_PERMISSION_LIST(state, data) {
    state.list = data.data
    state.page = data.meta ? data.meta.pagination : {}
  },

  SET_PERMISSION_DETAIL(state, data) {
    state.detail = data
  }

}

const actions = {

  /**
   * 获取列表
   * @param rootState
   * @param commit
   * @param query
   * @returns {Promise<unknown>}
   */
  getPermissionList({rootState, commit}, query = {}) {
    let data = Object.assign(query, rootState.route.query)
    data.sort = "id"
    data.dir = "desc"
    return new Promise((resolve, reject) => {
      permission.getList(data).then(ret => {
        commit('SET_PERMISSION_LIST', ret.data)
        resolve(ret)
      }).catch(ret => {
        reject(ret)

      })

    })

  },

  /**
   * 获取详情
   * @param commit
   * @param id
   * @returns {Promise}
   */
  getPermissionDetail({commit}, id) {
    return new Promise((resolve, reject) => {
      permission.show(id).then(ret => {
        let data = ret.data.data;
        commit('SET_PERMISSION_DETAIL', data)
        resolve(ret)

      }).catch(ret => {
        reject(ret)

      })

    })
  }

}

export default {
  state,
  actions,
  mutations
}
